package com.estore.service.persitence;

import com.estore.service.model.UserType;
import com.estore.service.persitence.core.MySQLDatabaseConnector;
import com.estore.service.util.exception.EStoreException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserTypeDatabaseManager {

    private Connection conn;
    private ResultSet result;

    public boolean insertDepartment(UserType userType) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement insertUserType = null;
        try {
            insertUserType = conn.prepareStatement("insert into usertype set name=?");
            insertUserType.setString(1, userType.getName());
            insertUserType.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace(System.out);
            inserted = false;
        }
        return inserted;
    }

    public boolean updateUserType(UserType userType) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement updateUserType = null;
        try {
            updateUserType= conn.prepareStatement("update usertype set "
                    + " name=? where id=?");
             updateUserType.setString(1, userType.getName());
            updateUserType.setInt(2, userType.getId());
            updateUserType.executeUpdate();
        } catch (SQLException e) {            
            inserted = false;
           throw new EStoreException(e);
        }
        return inserted;
    }

   

    public UserType getUserTypeById(int id) {
        UserType userType = new UserType();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectUserTypeById = null;
        try {
            selectUserTypeById = conn.prepareStatement("select * from usertype where id=?");
            selectUserTypeById.setInt(1,id);
            result = selectUserTypeById.executeQuery();
            while (result.next()) {
                userType = getUserType(result);
            }
        } catch (SQLException e) {
            throw new EStoreException(e);
        }
        return userType;
    }

    
    public UserType getUserType(ResultSet result) {
        UserType userType = new UserType();
        try {
            userType = new UserType();
            userType.setName(result.getString("name"));
            userType.setId(result.getInt("id"));
        } catch (SQLException e) {
            e.printStackTrace(System.out);
        }
        return userType;
    }

    public List<UserType> getAllUserTypes(int start, int size) {
        List<UserType> userTypes = new ArrayList<UserType>();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectAllUserTypes = null;
        try {
            selectAllUserTypes = conn.prepareStatement("select * from usertype LIMIT ?,?");
            selectAllUserTypes.setInt(1, start);
            selectAllUserTypes.setInt(2, size);
            result = selectAllUserTypes.executeQuery();
            while (result.next()) {
                userTypes.add(getUserType(result));
            }
        } catch (SQLException e) {
            throw new EStoreException(e);
        }
        return userTypes;
    }

    public int getAllUserTypeCount() {
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectAllUserTypeCount = null;
        try {
            selectAllUserTypeCount = conn.prepareStatement("select count(*) from usertype");
            result = selectAllUserTypeCount.executeQuery();
            while (result.next()) {
                return result.getInt(1);
            }
        } catch (SQLException e) {
            throw new EStoreException(e);
        }
        return 0;

    }
}
